#include "singupmainwindow.h"
#include <QDebug>
#include <QSqlError>
#include <QMessageBox>
SingUpMainWindow::SingUpMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SingUpMainWindow)
{
    ui->setupUi(this);
    // 去掉窗口标题栏
    this->setWindowFlag(Qt::FramelessWindowHint);

        // 禁止窗口改变尺寸大小
     this->setFixedSize(this->width(),this->height());

    //光标定位
    ui->textEdit->setFocus();
}

SingUpMainWindow::~SingUpMainWindow()
{
    delete ui;
}

void SingUpMainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //更改背景图片
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/new/prefix1/images/singup.jpg"));
}

void SingUpMainWindow::on_pushButton_2_clicked()
{
    this->close();
}

void SingUpMainWindow::on_pushButton_clicked()
{
        sql_Init(); //数据库初始化
        //shared_data::ShareData::client_choice=0; //注册标识
        QString username = ui->textEdit->toPlainText();
        QString password = ui->textEdit_2->toPlainText();
        QString surepass = ui->textEdit_3->toPlainText();
       // 判断密码是否一致
            if (password == surepass) {
                // 准备SQL语句
                QSqlQuery query;
                //QString hashedPassword = hashPassword(password);
                query.prepare("INSERT INTO infor(username, password) VALUES(:username, :password)");
                query.bindValue(":username", username);
                query.bindValue(":password", password); // 注意：这里直接存储明文密码是不安全的，应该存储哈希值

                // 执行SQL语句
                if (!query.exec()) {
                    qDebug() << "Error: Could not insert into database";
                    qDebug() << query.lastError().text(); // 输出错误信息
                    QMessageBox::information(this, "注册认证", "插入失败！");
                } else {
                    qDebug() << "Inserted successfully";
                    QMessageBox::information(this, "注册认证", "插入成功！");

                    //MainWindow *w = new MainWindow;
                    //w->show();
                    this->close();
                }

            }
            else {
                QMessageBox::information(this, "注册认证", "两次密码输入不一致");
            }
}

void SingUpMainWindow::sql_Init()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost"); // 设置MySQL服务器地址
        db.setDatabaseName("account_information"); // 设置数据库名
        db.setUserName("root"); // 设置数据库用户名
        db.setPassword("083911Aa"); // 设置数据库密码

        if (!db.open()) {
            qDebug() << "Database connection error";
            return; // 如果无法打开数据库连接，则退出函数
        }

        // 创建表
        QString createsql = QString("CREATE TABLE IF NOT EXISTS infor ("
                                 "id INT AUTO_INCREMENT PRIMARY KEY,"
                                 "username VARCHAR(20) UNIQUE NOT NULL,"
                                 "password VARCHAR(20) NOT NULL)");

        QSqlQuery query;
        if (!query.exec(createsql)) {
            qDebug() << "Table create error:" << query.lastError().text();
        } else {
            qDebug() << "Table create success";
        }
}
